$(document).ready(function () {
    var validate = $("#editForm").validate({
        debug: false,
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function (form) {
            var jsonData = $(form).serializeObject();
            $.ajax({
                url: "EditBonus",
                method: "POST",
                contentType: "application/json;charset=UTF-8",
                async: true,
                dataType: "json",
                data: JSON.stringify(jsonData),
                timeout: timeout,
                success: function (resp) {
                    alert(resp.Message);
                    if (resp.Result) {
                        select("list");
                        query("keep=true", "QueryBonus");
                    }
                }
            }).fail(function (K, J) {
                alert("浏览器超时未响应，请刷新重试");
            });
            return false;
        },

        rules: {
            CrewName: {
                required: true
            },
            Date: {
                required: true
            },
            Value: {
                required: true,
                range: [1, 2000000000]
            }
        },
        messages: {
            CrewName: {
                required: "服务人员不能为空"
            },
            Date: {
                required: "日期不能为空"
            },
            Value: {
                required: "金额不能为空",
                range: "请输入{0}-{1}之间的数字"
            }
        }

    });

    function A(H) {
        var I = $("div#list table.list tbody");
        I.empty();
        I.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "BonusPage",
            dataType: "json",
            data: "page=" + H,
            timeout: timeout,
            success: function (J) {
                update(J);
            }
        }).fail(function (K, J) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var pagerBonus = {
        maxRows: 0,
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: A
    };
    $("div#lpager").paginate(pagerBonus);
    window.update = function (I) {
        var L = $("div#list table.list tbody");
        var J = I.Items;
        $("div#list table.list input.selall").prop("checked", false);
        L.empty();
        if (J.length > 0) {
            for (var H = 0; H < J.length; H++) {
                var K = J[H];
                var M = K.Note || "";
                L.append('<tr><td><input name=ids class="row" type="checkbox" value="' + K.Id + '"/></td><td>' + K.CrewName + "</td><td>" + K.Date + "</td><td>" + K.Item + "</td><td>" + K.Value + "</td><td>" + M + '</td><td><button data-id="' + K.Id + '" class="edit btn btn-success"><i class="fa fa-edit fa-lg"></i> 编 辑</button></td></tr>');
            }
        } else {
            L.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }

        pagerBonus.maxRows = I.MaxRows;
        pagerBonus.start = I.Page;
        pagerBonus.count = I.Total;
        $("div#lpager").paginate(pagerBonus);
    };

    function F(H) {
        $("div#edit input").val("");
        if (H) {
            $("div#edit input.Id").val(H.Id);
            $("div#edit input.CrewId").val(H.CrewId);
            $("div#edit input.CrewName").val(H.CrewName);
            $("div#edit input.Date").val(H.Date);
            $("div#edit input.Item").val(H.Item);
            $("div#edit input.Value").val(H.Value);
            $("div#edit input.Note").val(H.Note);
        }
    }
    $("div#list div.query button.query").click(function (H) {
        query($("div#list div.query form").serialize(), "QueryBonus");
        return false;
    });
    query($("div#list div.query form").serialize(), "QueryBonus");
    $("ul.tool button.add").click(function (H) {
        F(null);
        select("edit");
        return false;
    });

    $("button.cancel").click(function (H) {
        validate.resetForm();
        select("list");
        return false;
    });
    $("ul.tool button.delete").click(function (H) {
        if (confirm("您确定要删除所有选中的记录吗？")) {
            var I = $("div#list table.list tbody input.row:checked").serialize();
            $.ajax({
                url: "DeleteBonuses",
                dataType: "json",
                data: I,
                timeout: timeout,
                success: function (J) {
                    alert(J.Message);
                    if (J.Result) {
                        query("keep=true", "QueryBonus");
                    }
                }
            }).fail(function (K, J) {
                alert("浏览器超时未响应，请刷新重试");
            });
        }
    });
    $("div#list table.list tbody").click(function (H) {
        var I = $(H.target);
        if (I.is("button.delete")) {
            if (confirm("您确定要删除该记录吗？")) {
                var J = I.data("id");
                $.ajax({
                    url: "DeleteBonus",
                    dataType: "json",
                    data: "Id=" + J,
                    timeout: timeout,
                    success: function (K) {
                        alert(K.Message);
                        if (K.Result) {
                            query("keep=true", "QueryBonus");
                        }
                    }
                }).fail(function (L, K) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            }
        } else {
            if (I.is("button.edit")) {
                var J = I.data("id");
                $.ajax({
                    url: "GetBonus",
                    dataType: "json",
                    data: "Id=" + J,
                    timeout: timeout,
                    success: function (K) {
                        F(K);
                        select("edit");
                    }
                }).fail(function (L, K) {
                    alert("浏览器超时未响应，请刷新重试");
                });
            }
        }
    });

    function updateCrew(K) {
        var H = $("div#crew table.list tbody");
        var Q = K.Items;
        H.empty();
        if (Q.length > 0) {
            for (var N = 0; N < Q.length; N++) {
                var J = Q[N];
                var R = J.Skill || "";
                var P = J.IdNo || "";
                var L = J.TrcNo || "";
                var I = J.HealthNo || "";
                var M = J.LicenseNo || "";
                var O = J.Tel || "";
                H.append("<tr><td>" + J.Name + "</td><td>" + J.Type + "</td><td>" + R + "</td><td>" + J.Status + "</td><td>" + P + "</td><td>" + L + "</td><td>" + I + "</td><td>" + M + "</td><td>" + O + '</td><td><button data-id="' + J.Id + '" class="select btn btn-success"><i class="fa fa-check fa-lg"></i> 选 择</button></td></tr>');
            }
        } else {
            H.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        pagerCrew.maxRows = K.MaxRows;
        pagerCrew.start = K.Page;
        pagerCrew.count = K.Total;
        $("div#wpager").paginate(pagerCrew);
    }
    function E(H) {
        var I = $("div#crew table.list tbody");
        I.empty();
        I.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "CrewPage",
            dataType: "json",
            data: "page=" + H,
            timeout: timeout,
            success: function (J) {
                updateCrew(J);
            }
        }).fail(function (K, J) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var pagerCrew = {
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: E
    };

    function queryCrew(I, H) {
        var J = $("div#crew table.list tbody");
        J.empty();
        J.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: H,
            dataType: "json",
            data: I,
            timeout: timeout,
            success: function (K) {
                updateCrew(K);
            },
            error: function (L, K, M) {
                alert("浏览器超时未响应，请刷新重试");
            }
        });
    }
    $("div#crew div.query button.query").click(function (H) {
        queryCrew($("div#crew div.query form").serialize(), "QueryCrew");
        return false;
    });
    $("div#edit button#CrewBrowser").click(function (H) {
        $("div#crew div.query form").resetForm();
        $("div#crew div.query").find('select[name="Status"]').val('在职');
        $("div#crew").dialog("open");
        $("div#wpager").paginate(pagerCrew);
        queryCrew(null, "QueryCrew");
        return false;
    });
    $("div#crew table.list tbody").click(function (H) {
        var I = $(H.target);
        if (I.is("button.select")) {
            var J = I.data("id");
            $.ajax({
                url: "GetCrew",
                dataType: "json",
                data: "Id=" + J,
                timeout: timeout,
                success: function (K) {
                    $("div#edit input.CrewId").val(J);
                    $("div#edit input.CrewName").val(K.Name);
                    $("div#edit input.CrewTel").val(K.Tel);
                    $("div#crew").dialog("close");
                }
            }).fail(function (L, K) {
                alert("浏览器超时未响应，请刷新重试");
            });
        }
    });

});